<template>
  <basic-container>
    <avue-crud
      :data="data"
      :option="option"
      :page.sync="page"
      @on-load="rowLoad"
      @search-change="searchChange"
      @refresh-change="rowLoad(page)"
      @row-update="rowUpdate"
      @row-del="rowDel"
      @row-save="rowSave"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
      <template slot-scope="{ row, type, size }" slot="menu">
        <el-button
          icon="el-icon-check"
          :size="size"
          :type="type"
          @click="setDictItem(row)"
        >
          字典配置
        </el-button>
      </template>
    </avue-crud>

    <Item ref="dictItem" />
  </basic-container>
</template>

<script>
import avue from "@/mixins/avue.js";
import { SE, NY } from "@/common/constants.js";
import Item from "./item";
import { dict_page, dict_add, dict_edit, dict_delete } from "@/api/core/system";

export default {
  mixins: [avue],
  components: { Item },
  data() {
    return {
      itemDrawer: false,
      itemData: [],
      menuProps: {
        label: "name",
      },
      menuDataRole: [],
      curRole: {},
      data: [],
      option: {
        dialogDirection: "rtl",
        dialogType: "drawer",
        column: [
          {
            label: "字典名称",
            prop: "name",
            align: "left",
            search: true,
          },
          {
            label: "字典编码",
            prop: "code",
            align: "left",
            search: true,
          },
          {
            label: "是否树形",
            prop: "isItemTree",
            type: "radio",
            dicData: NY,
            value: "0",
            hide: true,
          },
          {
            label: "状态",
            prop: "status",
            type: "radio",
            dicData: SE,
            value: "1",
            search: true,
          },
        ],
      },
    };
  },
  methods: {
    setDictItem(row) {
      this.$refs.dictItem.dictDetail(row.id);
    },
    rowLoad() {
      dict_page(this.params).then((res) => {
        this.data = this.$s.res(res).records;
      });
    },
    rowSave(row, done, loading) {
      dict_add(row).then((res) => {
        this.$s.suc(res);
        this.rowLoad(this.params);
        loading();
        done();
      });
    },
    rowUpdate(row, index, done, loading) {
      dict_edit(row).then((res) => {
        this.$s.suc(res);
        this.rowLoad(this.params);
        loading();
        done();
      });
    },
    rowDel(row, index) {
      this.$confirm("确定数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        dict_delete({ ids: row.id }).then((res) => {
          this.$s.suc(res);
          this.rowLoad(this.params);
        });
      });
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.footer {
  display: flex;
}
</style>
